<?php

namespace app\models;

use app\models\searchs\ClientFilterInterface;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Product;
use yii\data\DataFilter;

/**
 * ProductSearch represents the model behind the search form of `app\models\Product`.
 */
class ProductSearch extends Product implements ClientFilterInterface

{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'stock_num', 'classify_id', 'created_at', 'updated_at'], 'integer'],
            [['title', 'description'], 'safe'],
            [['price'], 'number'],
        ];
    }

    public function filters()
    {
        return [
            'id' => ['eq' => 0],
            'title' => ['like' => ''],
            'description' => ['like' => ''],
            'price' => ['eq' => 0],
            'stock_num' => ['eq' => 0],
            'classify_id' => ['eq' => 0],
            'created_at' => ['eq' => 0],
            'updated_at' => ['eq' => 0],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'Id'),
            'title' => Yii::t('app', 'Title'),
            'description' => Yii::t('app', 'Description'),
            'price' => Yii::t('app', 'Price'),
            'stock_num' => Yii::t('app', 'Stock Num'),
            'classify_id' => Yii::t('app', 'Classify Id'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }

    public function attributeTypes()
    {
        return [
            'id' => DataFilter::TYPE_INTEGER,
            'title' => DataFilter::TYPE_STRING,
            'description' => DataFilter::TYPE_STRING,
            'price' => DataFilter::TYPE_FLOAT,
            'stock_num' => DataFilter::TYPE_INTEGER,
            'classify_id' => DataFilter::TYPE_INTEGER,
            'created_at' => DataFilter::TYPE_INTEGER,
            'updated_at' => DataFilter::TYPE_INTEGER,
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Product::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => ['defaultOrder' => ['id' => SORT_DESC]]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'price' => $this->price,
            'stock_num' => $this->stock_num,
            'classify_id' => $this->classify_id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'description', $this->description]);

        return $dataProvider;
    }
}
